MsgN("-- entity.lua loaded")

local _ent = FindMetaTable("Entity")

--returns true or false if the model matches the entity (used for other functions)
function _ent:IsModelMatch( tbl )
	if (!self) || (!ValidEntity(self)) then return false end

	local model = string.lower(self:GetModel())

	for _, _model in pairs( tbl or {} ) do
		_model = string.lower(_model)

		if (model == _model) or (model == string.gsub(_model,"/","\\")) then
			return true
		end
	end

	return false
end

--checks model to see if its a tree
function _ent:IsTree()
	return self:IsModelMatch( MDRP.Seeds.Tree )
end

--checks to see if the model is a log
function _ent:IsLog()
	return self:IsModelMatch( MDRP.Logs )
end

--checks model to see if its food
function _ent:IsFood()
	return self:IsModelMatch( MDRP.Seeds.Food )
end

--checks model to see if its a rock
function _ent:IsRock()
	return self:IsModelMatch( MDRP.Seeds.Rock )
end

--returns the material of the object (Wood, Iron, Glass, etc)
function _ent:GetMaterialType()
	local phys = self:GetPhysicsObject()

	if (!phys) or (!phys:IsValid()) then return end

	local mat = phys:GetMaterial()

	return MDRP.Materials[mat]
end

function _ent:GetVolume()
	--if already computed return the volume
	if (self.Volume) then return self.Volume end

	--get the prop size
	local min, max = self:OBBMins(), self:OBBMaxs()

	--save the volume as cubit feet
	self.Volume = (math.abs(max.x-min.x) * math.abs(max.y-min.y) * math.abs(max.z-min.z)/(16^3))

	--return the volume
	return self.Volume
end

--custom ignite functions. default one uses smoke and it lags bad!
function _ent:Ignite( duration, radius )
	local dur = (duration or 10)
	local rad = (radius or 30)
	
	--remove fire if it already exists
	if (self._fire) and (self._fire:IsValid()) then self._fire:Remove() end

	--env_fire
	self._fire = ents.Create("env_fire")
	self._fire:SetParent( self )
	self._fire:SetPos( self:GetPos() )
	self._fire:SetKeyValue( "targetname", "fire" )
	self._fire:SetKeyValue( "health", dur )
	self._fire:SetKeyValue( "fireattack", "1000" )		--<integer> Amount of time the fire takes to grow to full strength. Set higher to make the flame build slowly.
	self._fire:SetKeyValue( "firesize", 10 )
	self._fire:SetKeyValue( "StartDisabled", "0" )
	self._fire:SetKeyValue( "spawnflags", tostring(2 + 4 + 16 + 128 + 256) )
	self._fire:Spawn()
	self._fire:Fire("StartFire","1","0")
end

function _ent:Extinguish()
	if (self._fire) and (self._fire:IsValid()) then self._fire:Remove() end
end

function _ent:IsOnFire()
	if (self._fire) and (self._fire:IsValid()) then
		return true
	else
		return false
	end
end